//package com.personal.gateway.job;
//
//import com.alibaba.fastjson.JSONObject;
//import com.personal.gateway.config.ValueConfig;
//import com.personal.gateway.custom.customEnum.FlinkCommandEnum;
//import com.personal.gateway.dto.ContainerGroup;
//import com.personal.gateway.entity.FlinkGatewayRoute;
//import com.personal.gateway.mapper.FlinkGatewayRouteMapper;
//import com.personal.gateway.service.JobService;
//import com.personal.gateway.service.K8sService;
//import com.personal.gateway.utils.MailUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections4.CollectionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//
//import javax.annotation.Resource;
//import java.util.List;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.stream.Collectors;
//
///**
// * @program: FlinkGateway
// * @description: 报警通知
// * @author: yangliao
// * @create: 2021-10-15
// */
//@Slf4j
//@Configuration
//@EnableScheduling
//public class AlarmNoticeTask {
//
//    private static Integer LOCK = 0;
//
//    @Resource
//    private ValueConfig valueConfig;
//
//    @Resource
//    private K8sService k8sService;
//
//    @Resource
//    private JobService jobService;
//
//    @Resource
//    private FlinkGatewayRouteMapper flinkGatewayRouteMapper;
//
//    /**
//     * 存储报警间隔
//     */
//    private static final Map<String, Long> TESTING_INTERVAL = new ConcurrentHashMap<>();
//
//    /**
//     * 每五分钟检测一次
//     */
//    @Scheduled(cron = "0 0/5 * * * ?")
//    private void alarmTasks() {
//        if (LOCK > 0) {
//            log.info("上次任务未执行完成跳出任务");
//            return;
//        }
//        LOCK = 1;
//
//        try {
//            detection();
//        } catch (Exception e) {
//            log.error("", e);
//        } finally {
//            LOCK = 0;
//        }
//    }
//
//
//    private void detection() {
//        // 1、获取所有配置 过滤需要进行报警的数据
//        List<FlinkGatewayRoute> routes = flinkGatewayRouteMapper.list();
//        routes = routes.stream().filter(x -> x.getAlarm_status().equals(1)).collect(Collectors.toList());
//        if (CollectionUtils.isEmpty(routes) || routes.size() == 0) {
//            return;
//        }
//
//        log.info("AlarmNoticeTask 需要报警的服务数量 {}", routes.size());
//
//        // 2、获取已经启动的服务
//        List<ContainerGroup> containerGroups = k8sService.getContainerGroup();
//
//        for (FlinkGatewayRoute route : routes) {
//            String route_id = route.getRoute_id();
//            long num = containerGroups.stream()
//                    .filter(x -> x.getComponent().equals("taskmanager") && x.getApp().equals(route_id))
//                    .count();
//            if (num < 1) {
//                // 判断是否需要报警
//                Long time = TESTING_INTERVAL.get(route_id);
//                Long local_time = System.currentTimeMillis() / 1000;
//                if (null != time) {
//                    long difference = local_time - time;
//                    if (difference <= (route.getTesting_interval() * 60)) {
//                        // 时间未到不通知、启动
//                        log.info("AlarmNoticeTask 时间为刷新不发送 {}", route.getRoute_id());
//                        continue;
//                    }
//                }
//                TESTING_INTERVAL.put(route_id, local_time);
//
//                // 3、启动服务
//                String exec = jobService.startNoSavePoint(route_id);
//                JSONObject obj = new JSONObject();
//                obj.put("id", route.getId());
//                obj.put("type", FlinkCommandEnum.START_NO_SAVE_POINT.getValue());
//                obj.put("command", exec);
//                String req = jobService.execute(obj, "alarm-task");
//
//                // 4、发送报警邮件通知
//                String alarm_email = route.getAlarm_email();
//                log.info("AlarmNoticeTask 发送邮件通知 01 {} {}", route.getRoute_id(), alarm_email);
//                if (StringUtils.isBlank(alarm_email)) {
//                    continue;
//                }
//
//                String sender = "Flink程序报警";
//                String subject = valueConfig.getActiveStr() + "环境Flink程序报警";
//                String content = "项目：" + route_id + "<br/><br/>" +
//                        "启动命令：" + exec + "<br/><br/>"+
//                        "返回结果：" + req + "<br/><br/>";
//                for (String email : alarm_email.split(",")) {
//                    try {
//                        MailUtils.sendMail(sender, subject, content, email);
//                    } catch (Exception e) {
//                        log.error("", e);
//                    }
//                }
//            }
//        }
//
//    }
//
//
//}
